/*
 * AndroidServerView.java
 */

package androidserver;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.ActionMap;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;

/**
 * The application's main frame.
 */
public class AndroidServerView extends FrameView {
	private AndroidServerApp app;
	
    private JButton sendBSButton;
    private JButton jButton2;
    private JButton jButton3;
    private JButton jButton4;
    private JButton jButton5;
    private JButton sendMessageButton;
    
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu helpMenu;
   
    private JScrollPane jScrollPane1;
    private JTextArea console;
    private JPanel mainPanel;
    
    private JProgressBar progressBar;
    private JLabel statusAnimationLabel;
    private JLabel statusMessageLabel;
    private JPanel statusPanel;

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
	private AndroidServerSettingBox settingBox;
    
    public AndroidServerView(SingleFrameApplication app) {
        super(app);
        this.app = (AndroidServerApp)app;
        
        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = AndroidServerApp.getApplication().getMainFrame();
            aboutBox = new AndroidServerAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        AndroidServerApp.getApplication().show(aboutBox);
    }
    
    @Action
    public void showSettingBox() {
        if (settingBox == null) {
            JFrame mainFrame = AndroidServerApp.getApplication().getMainFrame();
            settingBox = new AndroidServerSettingBox(mainFrame);
            settingBox.setLocationRelativeTo(mainFrame);
        }
        AndroidServerApp.getApplication().show(settingBox);
    }

    @Action
    public void sendBaseStation(){
    	if( settingBox==null )
    		JOptionPane.showConfirmDialog(null, "Please setting base station first!", "Note", JOptionPane.CLOSED_OPTION);
    	else {
    		Set<String> set = settingBox.getBaseStation().keySet();
    		app.server.setBaseStations( settingBox.getBaseStation() );
    		app.server.sendBaseStations2Phones( set );
    	}
    }
    
    @Action
    public void sendMessage(){
    	String message = console.getText().trim();
    	if( message.length()==0 )
    		JOptionPane.showConfirmDialog(null, "Please input message first!", "Note", JOptionPane.CLOSED_OPTION);
    	else {
    		app.server.sendMessage( message );
    	}
    }
    
    private void initComponents() {
    	ResourceMap resourceMap = Application.getInstance(androidserver.AndroidServerApp.class).getContext().getResourceMap(AndroidServerView.class);
    	ActionMap actionMap = Application.getInstance(androidserver.AndroidServerApp.class).getContext().getActionMap(AndroidServerView.class, this);
    	
    	menuBar = new JMenuBar();
    	menuBar.setName("menuBar"); // NOI18N
    	
    	fileMenu = new JMenu();
    	fileMenu.setText( resourceMap.getString("fileMenu.text") ); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N
        menuBar.add(fileMenu);
        
        JMenuItem settingMemuItem = new JMenuItem();
    	settingMemuItem.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
    	settingMemuItem.setAction( actionMap.get("showSettingBox") );
        settingMemuItem.setName("jMenuItem1"); // NOI18N
        fileMenu.add(settingMemuItem);
    	
        JMenuItem exitMenuItem = new JMenuItem();
        exitMenuItem.setAction( actionMap.get("quit") ); // NOI18N
        exitMenuItem.setText( resourceMap.getString("exitMenuItem.text") ); // NOI18N
        exitMenuItem.setName( "exitMenuItem" ); // NOI18N
        fileMenu.add(exitMenuItem);
        
    	helpMenu = new JMenu();
    	helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N
        menuBar.add(helpMenu);
        
    	JMenuItem aboutMenuItem = new JMenuItem();
    	aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        helpMenu.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        helpMenu.setName("jMenu2"); // NOI18N
        menuBar.add(helpMenu);
        
        console = new JTextArea();
        console.setColumns(20);
        console.setRows(5);
        console.setName("jTextArea1"); // NOI18N
        
        jScrollPane1 = new JScrollPane();
        jScrollPane1.setName("jScrollPane1");
        jScrollPane1.setViewportView(console);
        
        sendBSButton = new JButton();
        sendBSButton.setAction( actionMap.get("sendBaseStation") ); 
        sendBSButton.setName("jButton1"); // NOI18N
        
        jButton2 = new JButton();
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        
        jButton3 = new JButton();
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        
        jButton4 = new JButton();
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N
        
        jButton5 = new JButton();
        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        
        sendMessageButton = new JButton();
        sendMessageButton.setAction( actionMap.get("sendMessage") ); 
        sendMessageButton.setName("jButton6"); // NOI18N
       
       
        
        JSeparator statusPanelSeparator = new JSeparator();
        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
        
        statusMessageLabel = new javax.swing.JLabel();
        statusMessageLabel.setName("statusMessageLabel"); // NOI18N
        
        statusAnimationLabel = new javax.swing.JLabel();
        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
        
        progressBar = new javax.swing.JProgressBar();
        progressBar.setName("progressBar"); // NOI18N
       
        mainPanel = new JPanel();
        mainPanel.setName("mainPanel");
        GroupLayout mainPanelLayout = new GroupLayout(mainPanel);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                            .addComponent(sendBSButton)
                            .addComponent(jButton3))
                        .addGap(18, 18, 18)
                        .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addGroup(mainPanelLayout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addGap(18, 18, 18)
                                .addComponent(sendMessageButton))
                            .addGroup(GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                                .addComponent(jButton5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton4)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 123, Short.MAX_VALUE)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(mainPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(sendBSButton)
                    .addComponent(jButton2)
                    .addComponent(sendMessageButton))
                .addGap(32, 32, 32)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(jButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        );

        mainPanel.setLayout(mainPanelLayout);

        statusPanel = new JPanel();
        statusPanel.setName("statusPanel"); // NOI18N
        GroupLayout statusPanelLayout = new GroupLayout(statusPanel);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 214, Short.MAX_VALUE)
                .addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );
        statusPanel.setLayout(statusPanelLayout);
        
        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }
}
